package com.example.administrator.hqq.utils;

import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.example.administrator.hqq.base.MyApplication;
import com.example.administrator.hqq.config.LoongsConfig;

/**
 * @name: HQQ
 * @describe: 获取高德定位信息
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/1 18:13
 * @remark:
 */
public class GetLocationUtils {
    private static AMapLocationClient mLocationClient = null;
    private static AMapLocationClientOption mLocationOption;
    private static AMapLocationClientOption option;
    private static String TAG=GetLocationUtils.class.getSimpleName();
    public static String getCityCode(){
       return SPUtils.getInstance().getString(LoongsConfig.city_code,"");
    }
    public static String getLocation(){
        return SPUtils.getInstance().getString(LoongsConfig.location,"");
    }
    public static void getloction(final Context context){
//声明AMapLocationClient类对象
//声明定位回调监听器
        AMapLocationListener mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation!=null)
                {

                    if (aMapLocation.getErrorCode() == 0) {
                        String location="";
                        //可在其中解析amapLocation获取相应内容。
                        String city = aMapLocation.getCity();
                        String cityCode = aMapLocation.getCityCode();
                        double longitude = aMapLocation.getLongitude();
                        double latitude = aMapLocation.getLatitude();
                        location=+longitude+","+latitude;
                        SPUtils.getInstance().put( LoongsConfig.city_code,cityCode);
                        SPUtils.getInstance().put(LoongsConfig.location,location);
                        Log.e(TAG, "onLocationChanged: "+"city++++"+city+"经度+++"+longitude+"维度+++"+latitude );
                    }else {
                        MyApplication.asyncUploadLog("高德","location Error, ErrCode:"
                                + aMapLocation.getErrorCode() + ", errInfo:"
                                + aMapLocation.getErrorInfo()+",UserID:"+LoongsConfig.getUsersIdSPUtils());
                        //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                        Log.e("AmapError","location Error, ErrCode:"
                                + aMapLocation.getErrorCode() + ", errInfo:"
                                + aMapLocation.getErrorInfo());
                    }
                }
                mLocationClient.stopLocation();
                mLocationClient.onDestroy();
            }
        };
//初始化定位
       mLocationClient = new AMapLocationClient(context.getApplicationContext());
//设置定位回调监听
       mLocationClient.setLocationListener(mLocationListener);
       //声明AMapLocationClientOption对象
        mLocationOption = null;
//初始化AMapLocationClientOption对象
       mLocationOption = new AMapLocationClientOption();
        option = new AMapLocationClientOption();
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        if(null != mLocationClient){
            mLocationClient.setLocationOption(option);
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            //获取一次定位结果：
            //该方法默认为false。
            mLocationOption.setOnceLocation(true);
            //获取最近3s内精度最高的一次定位结果：
            //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
            mLocationOption.setOnceLocationLatest(true);
            //设置是否返回地址信息（默认返回地址信息）
            mLocationOption.setNeedAddress(true);
            //设置是否允许模拟位置,默认为true，允许模拟位置
            mLocationOption.setMockEnable(true);
            //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
            mLocationOption.setHttpTimeOut(20000);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
        }
   }
}
